#include "index/attribute_index/attribute_disk_indexer/attribute_disk_indexer.h"

namespace hawking {
namespace indexlib {

Status AttributeDiskIndexer::Load(
    const std::string& attr_dir,
    FSFileType data_filetype,
    bool need_increase,
    size_t slot_count_for_update,
    size_t slot_bytes_for_update) {
    std::string datafile = butil::string_printf("%s/data_file", attr_dir.data());
    data_reader_ = (data_filetype == FSFileType::FSFT_MMAP) ? 
        std::make_unique<MmapFileReader>(datafile) :
        std::make_unique<BlockFileReader>(datafile);
    ret = data_reader_->Init();
    if (ret != Status::OK) {
        LOG(ERROR) << "FileReader for datafile " << datafile
            << " Init fail with " << static_cast<uint32_t>(ret)
            << " with data_filetype " << static_cast<int32_t>(data_filetype);
        return ret;
    }

    if (field_type_ > util::ColumnType::COLUMN_DOUBLE) {
        std::string offsetfile = butil::string_printf("%s/offset_file", attr_dir.data());
        offset_reader_ = std::make_unique<MmapFileReader>(offsetfile, need_increase);
        ret = offset_reader_->Init();
        if (ret != Status::OK) {
            LOG(ERROR) << "MmapFileReader for offsetfile " << offsetfile
                << " Init fail with " << static_cast<uint32_t>(ret);
            return ret;
        }

        auto varlen_metafile = butil::string_printf("%s/varlen_metafile", attr_dir.data());
        varlen_metafile_reader_ = std::make_unique<MmapFileReader>(varlen_metafile);
        ret = varlen_metafile_reader_->Init();
        if (ret != Status::OK) {
            LOG(ERROR) << "MmapFileReader for varlen_metafile " << varlen_metafile
                << " Init fail with " << static_cast<uint32_t>(ret);
            return ret;
        }
    }

    if (need_increase && field_type_ > util::ColumnType::COLUMN_DOUBLE) {
        InitDocUpdator_(slot_count_for_update, slot_bytes_for_update);
    }

    return Status::OK;
}

}
}